package com.mls.fivedistrict.controller.portal;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.News;
import com.mls.fivedistrict.server.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

/**
 * @author chenhua
 * @date 2018/6/5 15:33
 */
@RestController
@RequestMapping("/api/app")
public class NewsController extends BaseController {
    @Autowired
    NewsService newsService;

    /**
     * 分页查询列表
     *
     * @return
     */
    @GetMapping("/v1/newses")
    public ServiceResponse getNewsList() {
        PageFilter pageFilter = RequestHolder.getPageFilter();
        PageHelper.startPage(pageFilter.getPageNum(), pageFilter.getPageSize());
        List list = newsService.selectList(pageFilter.getWrapper());
        PageInfo<News> pageInfo = new PageInfo<News>(list, 5);
        return ServiceResponse.createSuccessResponse(pageInfo);
    }

    /**
     * 单个新闻详情
     *
     * @param newsId
     * @return
     */
    @GetMapping("/v1/news/{newsId}")
    public ServiceResponse getNewsDetail(@PathVariable Long newsId) {
        Optional.ofNullable(newsId).orElseThrow(() -> new CustomerException("要查询的新闻id不能为空!"));
        News news = newsService.selectDetail(newsId);
        if (null != news) {
            return ServiceResponse.createSuccessResponse(news);
        } else {
            return ServiceResponse.createErrorResponse("查无此新闻!");
        }
    }




}
